/*
 * @Author: zhangyang
 * @Date: 2022-06-11 17:23:32
 * @LastEditTime: 2022-06-12 16:06:02
 * @Description: 
 */
const { app, BrowserWindow } = require('electron');
const { join, resolve } = require('path');

function createWindow () {
  const win = new BrowserWindow({
    webPreferences: {
      preload: join(app.getAppPath(), 'preload.js')
    }
  });

  if (process.env.MODE === 'dev') {
    win.loadURL('http://localhost:3000');
  } else {
    win.loadFile('./dist/index.html');
  }

  // 跨域
  win.webContents.session.webRequest.onBeforeSendHeaders(
    (details, cbk) => {
      cbk({
        requestHeaders: {
          ...details.requestHeaders,
          origin: '*',
        }
      });
    }
  );
  win.webContents.session.webRequest.onHeadersReceived(
    (details, cbk) => {
      cbk({
        responseHeaders: {
          ...details.responseHeaders,
          'Access-Control-Allow-Origin': ['*']
        }
      });
    }
  );

  win.setMenu(null);

  if (process.env.MODE === 'dev') {
    // 启用调试工具
    win.webContents.openDevTools();
  }
}

app.whenReady().then(() => {
  createWindow();

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
